import app
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template
from models import PortfolioCategory, PortfolioItem, Feedback

class ManagePage(webapp.RequestHandler):
    def get(self):
        self.response.out.write(template.render('templates/manage/main.html', {}))
        
class PortfolioHandler(webapp.RequestHandler):
    def get(self):
        action = self.request.get("action")
        if action == "add":
            cats = PortfolioCategory.all()
            self.response.out.write(template.render(
                'templates/manage/portfolio_item.html', {'action': 'add', 'cats': cats}
                ))
        elif action == "edit":
            item = PortfolioItem.get_by_id(int(self.request.get("id"))) or self.error(404)
            cats = PortfolioCategory.all()
            self.response.out.write(template.render(
                'templates/manage/portfolio_item.html', {'action': 'edit', 'item': item, 'cats': cats}
                ))
        elif action == "delete":
            item = PortfolioItem.get_by_id(int(self.request.get("id"))) or self.error(404)
            self.response.out.write(template.render(
                'templates/manage/portfolio_item_confirm.html', {'item': item}
                ))
        else:
            items = PortfolioItem.all()
            self.response.out.write(template.render('templates/manage/portfolio.html', {'items': items}))
    
    def post(self):
        action = self.request.get("action")
        if action == "add":
            if not self.request.get("image"):
                self.redirect('/manage/portfolio')
            item = PortfolioItem()
            item.fill(self.request)
            item.put()
            self.redirect('/manage/portfolio')
        elif action == "edit":
            item = PortfolioItem.get_by_id(int(self.request.get("id"))) or self.error(404)
            item.fill(self.request)
            item.put()
            self.redirect('/manage/portfolio')
        elif action == "delete":
            item = PortfolioItem.get_by_id(int(self.request.get("id"))) or self.error(404)
            item.delete()
            self.redirect('/manage/portfolio')
        else:
            self.error(404)
            
class PortfolioCategoriesHandler(webapp.RequestHandler):
    def get(self):
        action = self.request.get("action")
        if action == "add":
            self.response.out.write(template.render(
                'templates/manage/portfolio_category.html', {'action': 'add'}
                ))
        elif action == "edit":
            item = PortfolioCategory.get_by_id(int(self.request.get("id"))) or self.error(404)
            self.response.out.write(template.render(
                'templates/manage/portfolio_category.html', {'action': 'edit', 'item': item}
                ))
        elif action == "delete":
            item = PortfolioCategory.get_by_id(int(self.request.get("id"))) or self.error(404)
            self.response.out.write(template.render(
                'templates/manage/portfolio_category_confirm.html', {'item': item}
                ))
        else:
            items = PortfolioCategory.all()
            self.response.out.write(template.render('templates/manage/portfolio_categories.html', {'items': items}))
    
    def post(self):
        action = self.request.get("action")
        if action == "add":
            if not self.request.get("title") or not self.request.get("name") :
                self.redirect('/manage/portfolio-cats')
                return
            item = PortfolioCategory()
            item.fill(self.request)
            item.put()
            self.redirect('/manage/portfolio-cats')
        elif action == "edit":
            if not self.request.get("title") or not self.request.get("name") :
                self.redirect('/manage/portfolio-cats')
                return
            item = PortfolioCategory.get_by_id(int(self.request.get("id"))) or self.error(404)
            item.fill(self.request)
            item.put()
            self.redirect('/manage/portfolio-cats')
        elif action == "delete":
            item = PortfolioCategory.get_by_id(int(self.request.get("id"))) or self.error(404)
            item.delete()
            self.redirect('/manage/portfolio-cats')
        else:
            self.error(404)
            
class FeedbackHandler(webapp.RequestHandler):
    def get(self):
        items = Feedback.get_top(100)
        self.response.out.write(template.render('templates/manage/feedback.html', {'items': items}))

def main():
    app.start(
        ('/manage', ManagePage),
        ('/manage/portfolio', PortfolioHandler),
        ('/manage/portfolio-cats', PortfolioCategoriesHandler),
        ('/manage/feedback', FeedbackHandler)
    )

if __name__ == "__main__":
    main()